﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using Point=System.Drawing.Point;

namespace Common
{
    public class AppWindow
    {
        public IntPtr Hwnd { get; private set; }
        public Rectangle ClientRect
        {
            get { return NativeApi.GetClientRect(Hwnd); }
        }
        public Rectangle Rect
        {
            get { return NativeApi.GetWindowRect(Hwnd); }
        }
        public Point Location
        {
            get { return NativeApi.GetHwndPos(Hwnd); }
            set { NativeApi.SetHwndPos(Hwnd, (int)value.X, (int)value.Y); }
        }
        public Size Size
        {
            get { return NativeApi.GetHwndSize(Hwnd); }
            set { NativeApi.SetHwndSize(Hwnd, (int)value.Width, (int)value.Height); }
        }
        public string Title
        {
            get { return NativeApi.GetHwndTitle(Hwnd); }
            set { NativeApi.SetHwndTitle(Hwnd, value); }
        }
        public AppWindow(IntPtr hwnd)
        {
            Hwnd = hwnd;
        }
        public static List<AppWindow> GetWindows()
        {
            var result = new List<AppWindow>();
            foreach (var hwnd in NativeApi.EnumHwnds())
                result.Add(new AppWindow(hwnd));
            return result;
        }
        public void BringWindowToFront()
        {
            NativeApi.BringWindowToFront(Hwnd);
        }
        public void Restore(Rectangle rect)
        {
            NativeApi.SetWindowPlacement(Hwnd,rect);
        }
        public void Close()
        {
            SendMessage(WM.CLOSE, 0, string.Empty);
        }
        private void SendMessage(WM msg, uint param1, string param2)
        {
            NativeApi.SendMessage(Hwnd, msg, param1, param2);
        }
        #region Operators
        public bool Equals(AppWindow obj)
        {
            if (obj == null)
            {
                return false;
            }
            else
            {
                return obj.Hwnd.Equals(Hwnd);
            }
        }
        #endregion
    }
}
